*===================================================================================*
* Ado-file: 	csxmtsac Version 1.0 
* Author: 		Chen Shao(陈志豪)
* Affiliation: 	Lanzhou University
* E-mail: 		chenzhh20@lzu.edu.cn
* License:      Mulan PSL v2
* Date: 		2025/2/17                                 
*===================================================================================*

capture program drop csxmtsac
program define csxmtsac
	version 16
	 //命令格式: csxmcs y x1 x2…
	syntax varlist(min=2) [if] [in],time(varname) [st(string)]
	di in blue "晨韶学妹向您报告："
preserve
	tsset `time'
	local y : word 1 of `varlist'
	foreach var in `varlist'{
		if "`var'" != "`y'"{
			local x `x' `var'
		}
	}
	capture which esttab //检测是否有esttab命令
    if _rc!=0{
        qui ssc install estout,replace
         //下载esttab命令用于输出
    }
	local cunzhao=0
	while `cunzhao'==0{
		local acif=0
		di as white _n _n "一、自相关检验："
		*Q检验
		qui reg `y' `x'
		estimates store OLS
		qui predict e1
		qui wntestq e1
		local p=r(p)
		if `p'<0.05{
			local result "存在自相关"
			local acif=`acif'+1
		}
		else if `p'>0.05{
			local result "无自相关"
		}
		di as white "1.Ljing-Box Q检验:`result';"
		*高阶的BG检验
		qui su e1
		local i=int(12*(r(N)/100)^(1/4))
		while `i'>0{
			qui reg e1 `x' l(1/`i').e1
			mat results =r(table)
			local pc=el(results,4,colnumb(results,"L`i'.e1"))
			if `pc'<0.05{
				qui estat bgo,lags(`i')
				mat pvalues =r(p)
				local p =el(pvalues,1,1)
				if `p'<0.05{
					local result "存在自相关"
					local acif=`acif'+1
				}
				else {
					local result "无自相关"
				}
				local i=-1
			}
			else if `i'==2{
				qui estat bgo
				mat pvalues =r(p)
				local p =el(pvalues,1,1)
				if `p'<0.05{
					local result "存在自相关"
					local acif=`acif'+1
				}
				else {
					local result "无自相关"
				}
				local i=-1
			}
			else {
				local i=`i'-1
			}
		}
		di in white "2.`r(lags)'阶BG检验:`result';"
		*普通BG检验
		qui estat bgo
		qui mat pvalues=r(p)
		local p =el(pvalues,1,1)
		while `p' <0.05{
			local result "存在自相关"
			local p=0.05
			local acif=`acif'+1
		}
		while `p' >0.05 {
			local result "无自相关"
			local p=0.05
		}
		di in white "3.以0取代缺失值,`r(lags)'阶BG检验:`result';"
		qui estat bgo,nom
		qui mat pvalues=r(p)
		local p =el(pvalues,1,1)
		while `p' <0.05{
			local result "存在自相关"
			local p=0.05
			local acif=`acif'+1
		}
		while `p' >0.05 {
			local result "无自相关"
			local p=0.05
		}
		di in white "4.不以0取代缺失值,`r(lags)'阶BG检验:`result';"
			
		*自相关的修正
		if `acif'!=0{
			di as white _n _n "二、自相关修正"
			*HAC标准误
			di as white "1.自相关修正：HAC标准误"
			qui su `y'
			local phac1 =round((r(N))^(1/4),1)
			local phac2 =round(0.75*r(N)^(1/3),1)
			if `phac1'!=`phac2'{
				qui newey `y' `x',lag(`phac1') 
				estimates store HAC1
				qui newey `y' `x',lag(`phac2') 
				estimates store HAC2
				esttab OLS HAC1 HAC1,b(3) se(2) sca(r2 N F lag) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			}
			else {
				qui newey `y' `x',lag(`phac1') 
				estimates store HAC
				esttab OLS HAC,b(3) se(2) sca(r2 N F lag) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			}
			
			*FGLS
			di as white _n "2.自相关修正：FGLS-PW估计"
			qui prais `y' `x',rho(reg)
			estimates store FGLS_PW1
			qui prais `y' `x',rho(freg)
			estimates store FGLS_PW2
			qui prais `y' `x',rho(tscorr)
			estimates store FGLS_PW3
			qui prais `y' `x',rho(dw)
			estimates store FGLS_PW4
			qui prais `y' `x',rho(theil)
			estimates store FGLS_PW5
			qui prais `y' `x',rho(nagar)
			estimates store FGLS_PW6
			esttab FGLS_PW*,b(3) se(2) sca(r2 N F rhotype) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			di as white _n "3.自相关修正：FGLS-CO估计"
			qui prais `y' `x',rho(reg) corc
			estimates store FGLS_CO1
			qui prais `y' `x',rho(freg) corc
			estimates store FGLS_CO2
			qui prais `y' `x',rho(tscorr) corc
			estimates store FGLS_CO3
			qui prais `y' `x',rho(dw) corc
			estimates store FGLS_CO4
			qui prais `y' `x',rho(theil) corc
			estimates store FGLS_CO5
			qui prais `y' `x',rho(nagar) corc
			estimates store FGLS_CO6
			esttab FGLS_CO*,b(3) se(2) sca(r2 N F rhotype) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
		}
		local cunzhao=1
	}
restore
di in blue _n "您布置的任务已经完成，请您过目~♥"
end